package com.losdun.common.sms;

import com.alibaba.fastjson2.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/9/4
 **/
@Data
@Slf4j
public class AliyunSmsClient {

    private final AliyunSmsProperties properties;
    private Client client;

    public AliyunSmsClient(AliyunSmsProperties properties) {
        this.properties = properties;
        this.client=createClient();
    }

    private Client createClient() {
        Config config = new Config()
                .setAccessKeyId(properties.getAccessKeyId())
                .setAccessKeySecret(properties.getAccessKeySecret())
                .setEndpoint(properties.getEndpoint());
        try {
            return new Client(config);
        } catch (Exception e) {
            log.error("阿里云短信client创建失败!msg:{}",e.getMessage(),e);
            throw new RuntimeException("阿里云短信client创建失败!",e);
        }
    }

    /**
     * 获取短信模版
     * @param type
     * @return
     */
    private String getTemplateCode(String type){
       return properties.getTemplateCodes().stream().filter(t -> t.getType().equals(type)).findFirst().map(t -> t.getCode()).orElse(null);
    }

    /**
     * 验证码短信
     * @param templateType
     * @param mobile
     * @param smsCode
     * @return
     */
    public String sendSms(String templateType,String mobile, String smsCode){
        AliyunCodeTemplateParam codeTemplateParam = new AliyunCodeTemplateParam();
        codeTemplateParam.setCode(smsCode);
        String templateCode = getTemplateCode(templateType);
        if(templateCode==null){
            log.error("类型为:{}的短信模版不存在",templateType);
            throw new RuntimeException("短信模版不存在!");
        }
        return sendSms(templateCode, properties.getSignName(),mobile, JSON.toJSONString(codeTemplateParam));
    }

    /**
     * 根据type发送短信
     * @param templateType
     * @param mobile
     * @param content
     * @return
     */
    public String sendSmsByType(String templateType,String mobile, String content){
        String templateCode = getTemplateCode(templateType);
        if(templateCode==null){
            log.error("类型为:{}的短信模版不存在",templateType);
            throw new RuntimeException("短信模版不存在!");
        }
        return sendSms(templateCode, properties.getSignName(),mobile, content);
    }

    /**
     * 模版短信 多个参数
     * @param templateCode
     * @param mobile
     * @param paramsMap
     * @return
     */
    public String sendSms(String templateCode, String mobile, Map<String,String> paramsMap){
        AliyunCodeTemplateParam codeTemplateParam = new AliyunCodeTemplateParam();
        if(paramsMap.get("code")!=null){
            codeTemplateParam.setCode(paramsMap.get("code"));
        }
        if(paramsMap.get("orderSn")!=null){
            codeTemplateParam.setOrderSn(paramsMap.get("orderSn"));
        }
        return sendSms(templateCode, properties.getSignName(), mobile, JSON.toJSONString(codeTemplateParam));
    }

    /**
     * 发送短信
     * @param templateCode
     * @param signName
     * @param mobile
     * @param content
     * @return
     */
    public String sendSms(String templateCode,String signName, String mobile, String content){
        try {
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName(signName)
                    .setTemplateCode(templateCode)
                    .setTemplateParam(content);
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            return sendSmsResponse.getBody().getCode();
        } catch (Exception _error) {
            log.error("发送短信错误:errMsg:{}",_error.getMessage(), _error);
        }
        return "FAILED";
    }
}
